package cn.kpt.search.service.impl;


import cn.kpt.beans.vo.hotel.SearchHotCityVO;
import cn.kpt.beans.vo.hotel.SearchHotelVO;
import cn.kpt.search.dao.SearchHotelDao;
import cn.kpt.search.entity.HotelSearchVO;
import cn.kpt.search.service.SearchHotelService;
import cn.kpt.utils.EmptyUtils;
import cn.kpt.utils.Page;
import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SearchHotelServiceImpl implements SearchHotelService {

    @Resource
    private SearchHotelDao searchHotelDao;

    @Override
    public List<HotelSearchVO> searchHotelListByHotCity(SearchHotCityVO vo) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("cityId", vo.getCityId());
        map.put("count", vo.getCount());
        return searchHotelDao.getHotelListByParams(map);
    }

    @Override
    public void searchHotelListForPaging(Page<HotelSearchVO> page, SearchHotelVO vo) throws Exception {
        // 初始化查询条件  设定目的地条件
        SolrQuery solrQuery = new SolrQuery("destination:" + vo.getDestination());
        // 关键字
        if (EmptyUtils.isNotEmpty(vo.getKeywords()) && !"null".equals(vo.getKeywords())) {
            solrQuery.addFilterQuery("keyword:" + vo.getKeywords());
        }
        // 价格（以最低价格展示）
        if (EmptyUtils.isNotEmpty(vo.getMinPrice()) && EmptyUtils.isNotEmpty(vo.getMaxPrice())) {
            String format = String.format("minPrice:[%s TO %s]", vo.getMinPrice(), vo.getMaxPrice());
            solrQuery.addFilterQuery(format);
        }
        // 星级
        if (EmptyUtils.isNotEmpty(vo.getHotelLevel())) {
            solrQuery.addFilterQuery("hotelLevel:" + vo.getHotelLevel());
        }

        if (EmptyUtils.isNotEmpty(vo.getFeatureIds())) {
            // 选择的字段越多，则要求越多 数据越来越少
            // 将前端传输的字符串进行切割
            String[] featureIds = vo.getFeatureIds().split(",");
            StringBuffer featureIdBuffer = new StringBuffer();

            if (featureIds.length > 1) {
                for (int i = 0; i < featureIds.length; i++) {
                    if (i == 0) {
                        featureIdBuffer.append("featureIds:*," + featureIds[i] + ",* ");
                    } else {
                        //AND或者OR
                        featureIdBuffer.append(" AND featureIds:*," + featureIds[i] + ",* ");
                    }
                }
            } else {
                // featureIds：*,117,* 之所以前后加上,号 是因为防止 117 包含 *17*
                featureIdBuffer.append("featureIds:*," + featureIds[0] + ",*");
            }

            solrQuery.setQuery(featureIdBuffer.toString());
        }

        // 排序
        // 最受欢迎   评分  价格  星级
        // ascSort:isOkCount
        if (EmptyUtils.isNotEmpty(vo.getAscSort())) {
            solrQuery.setSort(vo.getAscSort(), SolrQuery.ORDER.asc);
        }
        // descSort:isOkCount
        if (EmptyUtils.isNotEmpty(vo.getDescSort())) {
            solrQuery.setSort(vo.getDescSort(), SolrQuery.ORDER.desc);
        }
        long total = searchHotelDao.getHotelListCount(solrQuery);
        page.setTotal((int) total);
        // 分页
        solrQuery.setStart(page.getBeginPos());
        solrQuery.setRows(page.getPageSize());
        List<HotelSearchVO> rows = searchHotelDao.getHotelListForPaging(solrQuery);
        page.setRows(rows);
    }
}
